<?php
namespace app\index\controller;

use think\Session;
use think\Cache;

class Personal extends Ubase
{
	public function __construct()
	{
		parent::__construct();
		if (!Session::has('aid')) {
			Cache::rm('log');
			echo'<script>alert("请登录");window.location.href="/"</script>';
		}
	}

	public function index ()
	{
		$user = [];
		$groom = [];
		$setting = db('config')->where('key','sell_base')->value('value');
		$this->assign('stkey',$setting);
		$id = Session::get('aid');
		$groom = db('groom')->where('uid',$id)->find();
		$this->assign('groom',$groom);
        $g_title = db('notice')->order('create_time desc')->where('n_keywords',0)->field('n_title')->limit(3)->select();
        $d_title = db('notice')->order('create_time desc')->where('n_keywords',1)->field('n_title')->limit(3)->select();
        $n_title = db('notice')->order('create_time desc')->where('n_keywords',2)->field('n_title')->limit(3)->select();
        $this->assign('gnotice',$g_title);
        $this->assign('dnotice',$d_title);
        $this->assign('nnotice',$n_title);
        $allpays = db('receipt')->where('id',1)->find();
        $this->assign('allpays',$allpays);
        $arr_group = db('ns_user_group')->select();
        $group = [];
        foreach ($arr_group as $v){
        	$group[$v['id']] = $v['group_name'];
        }
        $this->assign('group',$group);
        if (Session::has('aid')) {
            $user = db('ns_user')->where('id',Session::get('aid'))->find();
	        $this->assign('user',$user);
	    }
	    return $this->fetch();
	}

	public function signin ()
	{
		$rst = [];
		$rst = db('signin')->where('uid',Session::get('aid'))->field('rec_time')->select();
		if ($rst) {
			foreach ($rst as $k=>$v) {
				$rst[$k] = $v['rec_time'];
			}
		}
		return $rst;
	}

	public function tosign ()
	{
		$where = array();
		$id = Session('aid');
		$were['uid'] = $id;
		$where['rec_time'] = date('Y-m-d');
		if (db('signin')->where($where)->count()) {
			$this->error('您已经签到过了哦，不可重复签到');
		} else {
			$user = db('ns_user')->where('id',$id)->find();
			$group = db('ns_user_group')->where('id',$user['group_id'])->find();
			if($user['perfect']){
				if ($user['total_funds']>0) {
					if (($group['sign_condition'] <= $user['total_funds'])) {
							$smofme = round($user['total_funds'] * $group['sign_reward']) / 100 ;
							// 签到日志
							$signin = [];
							$signin['uid'] = $id;
							$signin['smofme'] = $smofme;
							$signin['rec_time'] = date('Y-m-d');
							// 加入用户资金
							$money = [];
							$money['total_funds'] = $user['total_funds'] + $smofme;
							$money['available_funds'] = $user['available_funds'] + $smofme;
							
							// 判断等级是否达到升级标准，如果达到 升级
							$group_id = $user['group_id']+1;
							$quota = db('ns_user_group')->where('id',$group_id)->value('quota');
							if($money['total_funds']>=$quota) {
								$money['group_id'] = $group_id;
							}
							db('signin')->insert($signin);
							$rst = db('ns_user')->where('id',$id)->update($money);
							if ($rst) {
								$this->success('签到成功，您领取了'.$smofme.'元佣金');
							} else {
								$this->error('稍后重试');
							}
					}
					$this->error('您的资金小于'.$group['sign_condition'].'元，领取失败');
				}
				$this->error('您还没有资金，赶快来投资吧');
			}
			$this->error('请先完善您的资料');
		}
	}

	public function qrcode()
    {
		$groom = [];
		$groom['code'] = 0;
		$row = db('groom')->where('uid',Session::get('aid'))->find();
		if ($row) {
			$groom = $row;
			$groom['code'] = 1;
			return $groom;
		}else{
			// $as3 = substr_replace(substr($code,2),'',-10,10);
			$groom['uid'] = Session::get('aid');
			if (db('ns_user')->where('id',$groom['uid'])->value('perfect')) {
				$webPath = '/qrcode/';
				$savePath = APP_PATH . '/../Public/qrcode/';
				$groom['codepath'] = $_SERVER['HTTP_ORIGIN'].'?groom='.rand(0,9).rand(0,9).$groom['uid'].time();
				if($filename = createQRcode($savePath, $groom['codepath'])){
					$groom['gr_img'] = $webPath . $filename;
				}
				// return $groom;
				// unset($groom['code']);
				$rst = db('groom')->insert($groom);
				if ($rst) {
					$groom['code'] = 1;
					return $groom;
				} else {
					$this->error('请稍后再试');
				}
			} else {
				$this->error('请先完善资料');
			}
		}
	}

	public function upload() {
		$file = request()->file('file');
		$info = $file->validate(['size' => '4096000'])
			->move(ROOT_PATH . 'public/proof/');
		if ($info) {
			$path = '/proof/' . $info->getsavename();
			$path = str_replace('\\', '/', $path);
			return $data = array(
				'code' => 1,
				'msg' => '上传成功',
				'data' => $path,
			);
		} else {
			return $data = array(
				'msg' => $file->getError(),
				'code' => 0,
			);
		}
	}

	public function proof()
	{
		$post = input('post.');
		if ( $post['rec_paper'] ) {
			if (is_numeric($post['rec_paper'])) {
				if ($post['proofpic']) {
					$post['uid'] = Session::get('aid');
					$post['rec_num'] = time().rand(0,9).rand(0,9).rand(0,9).rand(0,9);
					$post['rec_time'] = date('Y-m-d H-i-s');
					if (db('recharge')->insert($post)) {
						$this->success('充值订单提交成功，等待管理员处理');
					}
					$this->error('请稍后再试');
				}
				$this->error('请上传图片');
			}
			$this->error('请输入有效数值');
		}	
		$this->error('请输入金额');
	}

	public function funds()
	{
		if(is_post()){
			$id = Session::get('aid');
			if($id){
				if(input('post.')){
					$funds =(float)input('post.funds');
					$user = db('ns_user')->where('id',$id)->find();
					if ($user['available_funds']>$funds) {
						$setting = db('config')->select();
						if(!($funds%$setting[9]['value'])){
							if ($setting[7]['value'] >= $funds) {
								$arr_time = [date('Y-m-d H:i:s',strtotime(date('Y-m-d'))),date('Y-m-d H:i:s')];
								$count = db('draw')
									-> where('uid',$id)
								    -> where('rec_time','between',$arr_time)
								    -> count();
								if ($setting[8]['value'] >= $count) {
									$info['uid'] = $id;
									$info['rec_num'] = time().rand(0,9).rand(0,9).rand(0,9).rand(0,9);
									$info['rec_paper'] = $funds;
									$info['sum'] = $funds-$funds * $setting[12]['value']/100;
									$info['rec_time'] = date('Y-m-d H:i:s');
									$info['bank_num'] = $user['bank_num'];
									$info['sell_base']  = $setting[12]['value'];
									if(db('draw')->insert($info)){
										$money = [];
										$money['total_funds'] = $user['total_funds']-$funds;
										$money['available_funds'] = $user['available_funds']-$funds;
										db('ns_user')->where('id',$id)->update($money);
										$records = [];
										$records['uid'] = $id;
										$records['acc_type'] = 2;
										$records['num'] = $funds;
										$records['note'] = '提现操作';
										$records['create_time'] = date('Y-m-d H:i:s');
										db('user_account_records')->insert($records);
										$this->success('提现订单已提交，我们将尽快处理');
									}
									$this->error('请稍后再试');
								}
							}
							$this->error('超过单笔限额');
						}
						$this->error('必须为'.$setting[9]['value'].'的整数倍');
						// return $setting;
						// if($funds>$setting)
					}
					$this->error('超出您的可用额度');
				}
				$this->error('请输入有效值');
			}
			$this->error('请重新登录');
		}
		$this-> error('这是一个非法请求哦');
	}
}